#include <windows.h> // Windows头文件
#include <stdio.h>
#include "SDL.h"
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include "sgl.h"
#include "../draw/sgl_draw_arc.h"
#include "../draw/sgl_draw_circle.h"


extern void update_win(SDL_Renderer * m_renderer);
extern int sdl_show_windows(SDL_Window **m_window, SDL_Renderer **m_renderer);

SDL_Event myEvent; //SDL事件

SDL_Window * m_window = NULL;
SDL_Renderer * m_renderer = NULL;


#define TEST_RADIUS 20


void btn_event(sgl_obj_t *obj, void *data)
{
    if(sgl_obj_get_event_status(obj) == SGL_EVENT_PRESSED) {
        printf("buttton pressed\n");
    }
    else if(sgl_obj_get_event_status(obj) == SGL_EVENT_RELEASED) {
        printf("buttton released\n");
    };
}

void sw_event(sgl_obj_t *obj, void *data)
{
    if(sgl_obj_get_event_status(obj) == SGL_EVENT_PRESSED) {
        if(sgl_switch_get_status(obj)) {
            printf("ON\n");
        }
        else {
            printf("OFF\n");
        }
    }
}

void chkbox_event(sgl_obj_t *obj, void *data)
{
    if(sgl_obj_get_event_status(obj) == SGL_EVENT_PRESSED) {
        
    }
}

void anim_cb(sgl_anim_t *anim, void *data)
{
    anim->obj->size.w ++;
}


Uint32 fb_update(Uint32 interval, void *param)//回调函数 
{
	update_win(m_renderer);
	return interval;
}

Uint32 sgl_system_tick(Uint32 interval, void *param)//回调函数 
{
	sgl_tick_inc(5);
	return interval;
}


const uint8_t test_bitmap[] {

0x00,0x00,0x00,0x3e,0xc0,0x00,0x00,0x00,  //.......@%.......
0x00,0x00,0x00,0xaf,0xe5,0x00,0x00,0x00,  //......%@@+......
0x00,0x00,0x00,0xee,0xec,0x00,0x00,0x00,  //......@@@%......
0x00,0x00,0x08,0xc8,0xae,0x50,0x00,0x00,  //.....+%+%@+.....
0x00,0x35,0x8e,0xe0,0x3e,0xe8,0x53,0x00,  //...++@@..@@++...
0xef,0xff,0xca,0x50,0x08,0xce,0xff,0xfe,  //@@@@%%+..+%@@@@@
0xae,0xc0,0x00,0x00,0x00,0x00,0x3e,0xe5,  //%@%..........@@+
0x08,0xec,0x00,0x00,0x00,0x03,0xee,0x50,  //.+@%........@@+.
0x00,0x8e,0xc0,0x00,0x00,0x3e,0xe5,0x00,  //..+@%......@@+..
0x00,0x0a,0xe3,0x00,0x00,0x8c,0x80,0x00,  //...%@.....+%+...
0x00,0x0c,0xe0,0x00,0x00,0x5e,0xa0,0x00,  //...%@.....+@%...
0x00,0x0e,0xc0,0x5c,0xc3,0x3e,0xc0,0x00,  //...@%.+%%..@%...
0x00,0x3e,0xcc,0xfc,0xee,0xce,0xe0,0x00,  //...@%%@%@@%@@...
0x00,0x5e,0xfc,0x50,0x05,0xef,0xe3,0x00,  //..+@@%+..+@@@...
0x00,0x5a,0x50,0x00,0x00,0x08,0xa3,0x00,  //..+%+......+%...

};

int main( int argc, char * argv[] )
{
    int quit = 0;
    if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0 )
        return -1;
    sdl_show_windows(&m_window, &m_renderer);
    
    sgl_init();
    
    sgl_icon_t btn_icon = {.bitmap = test_bitmap, .width = 16, .height = 15};
    sgl_style_t btn_style = {.body_color = SGL_BLUE, .text_color = SGL_RED, .radius = 25};
    sgl_obj_t* btn = sgl_button_create(sgl_scr_active());
    sgl_obj_set_size(btn, 100, 50);
    sgl_obj_set_align(btn, SGL_ALIGN_CENTER_RIGHT);
    sgl_obj_set_style(btn, btn_style);
    sgl_obj_set_event_cb(btn, btn_event, NULL);
    sgl_button_set_toggle_color(btn, SGL_GREEN);
    sgl_button_set_icon(btn, &btn_icon);
    sgl_button_set_font(btn, &song23);
    sgl_button_set_text(btn, "Button");

    sgl_style_t sw_style = {.body_color = rgb(255,0,255), .radius = 15};
    sgl_obj_t* sw = sgl_switch_create(sgl_scr_active(), true);
    sgl_obj_set_size(sw, 60, 30);
    sgl_obj_set_align(sw, SGL_ALIGN_CENTER_LEFT);
    sgl_obj_set_style(sw, sw_style);
    sgl_obj_set_event_cb(sw, sw_event, NULL);
    sgl_switch_set_toggle_color(sw, SGL_BLACK);
    sgl_switch_set_status(sw, false);

    sgl_style_t sw_style2 = {.body_color = SGL_BLUE, .radius = 10};
    sgl_obj_t* sw2 = sgl_switch_create(sgl_scr_active(), true);
    sgl_obj_set_size(sw2, 40, 20);
    sgl_obj_set_align(sw2, SGL_ALIGN_BOT_MID);
    sgl_obj_set_style(sw2, sw_style2);
    sgl_obj_set_event_cb(sw2, sw_event, NULL);
    sgl_switch_set_toggle_color(sw2, SGL_BLACK);
    sgl_switch_set_status(sw2, false);

    sgl_style_t label_style = {.body_color = SGL_BLUE, .text_color = SGL_WHITE, .radius = 15};
    sgl_obj_t* label = sgl_label_create(sgl_scr_active());
    sgl_obj_set_size(label, 200, 40);
    sgl_obj_set_pos(label, 10, 20);
    sgl_obj_set_radius(label, 8);
    sgl_obj_set_style(label, label_style);

    sgl_label_set_font(label, &song23);
    sgl_label_set_text(label, "Welcome to SGL");
    sgl_label_set_text_align(label, SGL_ALIGN_CENTER_RIGHT);
    sgl_obj_set_event_cb(label, chkbox_event, NULL);
    sgl_label_set_icon(label, &btn_icon);

    sgl_style_t bat_style = {.body_color = SGL_RED, .text_color = SGL_BLUE, .radius = 3};
    sgl_obj_t* bat = sgl_battery_create(sgl_scr_active(), 10);
    sgl_obj_set_style(bat, bat_style);
    sgl_obj_set_size(bat, 16, 24);
    sgl_obj_set_pos(bat, 100, 200);
    sgl_battery_set_border_weight(bat, 2);

    sgl_style_t chkbox_style = {.body_color = SGL_RED, .text_color = SGL_BLUE, .radius = 3};
    sgl_obj_t *chkbox = sgl_checkbox_create(sgl_scr_active(), true);
    sgl_obj_set_size(chkbox, 150, 15);
    sgl_obj_set_pos(chkbox, 50, 100);
    sgl_obj_set_event_cb(chkbox, chkbox_event, NULL);
    sgl_obj_set_style(chkbox, chkbox_style);
    sgl_checkbox_set_font(chkbox, &consolas15);
    sgl_checkbox_set_text(chkbox, "This is A");

    sgl_style_t radio_style = {.body_color = SGL_BLACK, .text_color = SGL_BLUE, .radius = 3};
    sgl_obj_t *radio = sgl_radiobtn_create(sgl_scr_active(), true);
    sgl_obj_set_size(radio, 150, 16);
    sgl_obj_set_pos(radio, 50, 250);
    sgl_obj_set_event_cb(radio, chkbox_event, NULL);
    sgl_obj_set_style(radio, radio_style);
    sgl_radiobtn_set_font(radio, &consolas15);
    sgl_radiobtn_set_text(radio, "This is B");

    sgl_style_t number_style = {.body_color = rgb(100,200,200), .text_color = SGL_BLUE, .radius = 5};
    sgl_obj_t *number = sgl_number_create(sgl_scr_active(), 12);
    sgl_obj_set_size(number, 40, 40);
    sgl_number_set_text_align(number, SGL_ALIGN_CENTER_RIGHT);
    sgl_number_set_font(number, &song23);
    sgl_obj_set_style(number, number_style);
    sgl_obj_set_pos(number, 10, 200);

    sgl_anim_t amin;
    sgl_anim_set_anim_cb(&amin, anim_cb, NULL);
    sgl_anim_set_times(&amin, 100);
    sgl_anim_set_interval(&amin, 5);
    sgl_anim_bind_obj(&amin, number);
    sgl_anim_exec(&amin);

    sgl_style_t pgs_style = {.body_color = rgb(100,200,200), .text_color = SGL_BLUE, .radius = 5};
    sgl_obj_t *pgsbar = sgl_pgsbar_create(sgl_scr_active(), 60);
    sgl_obj_set_size(pgsbar, 200, 10);
    sgl_obj_set_style(pgsbar, pgs_style);
    sgl_obj_set_pos(pgsbar, 10, 80);
    //sgl_pgsbar_set_value(pgsbar, 45);
    sgl_pgsbar_set_font(pgsbar, &consolas15);
    sgl_obj_set_event_cb(pgsbar, chkbox_event, NULL);


    // sgl_size_t buf = {.w = 80, .h = 80};
    // sgl_draw_point(buf, 40, 40, SGL_BLUE);
    // sgl_draw_buffer_clear(SGL_WHITE, buf);

    // sgl_draw_thick_arc(buf, 40, 40, 21, 25, 280, 6, SGL_BLUE, SGL_WHITE);
    // sgl_pos_t pos;
    // pos = sgl_figure_arc_endpoint(20, 340);
    // printf("pos.x = %d  pos.y = %d\n", pos.x, pos.y);
    // sgl_draw_buffer_flush(300, 40, buf);

    static uint8_t bat_qua = 0;
    SDL_TimerID timer = SDL_AddTimer(50, fb_update, NULL);//创立定时器timer
    SDL_TimerID timer_tick = SDL_AddTimer(5, sgl_system_tick, NULL);//创立定时器timer
    while (!quit)
    {
        sgl_task_handler();
        while (SDL_PollEvent(&myEvent))
        {
            switch (myEvent.type)
            {
            case SDL_QUIT:
                quit = 1;
                break;
            case SDL_MOUSEBUTTONDOWN:
                if(myEvent.button.button == SDL_BUTTON_LEFT) {
                    sgl_event_pos_t pos;
                    pos.x = myEvent.motion.x; // 获取鼠标X轴位置
                    pos.y = myEvent.motion.y; // 获取鼠标Y轴位置
                    pos.type = SGL_EVENT_PRESSED;
                    sgl_event_queue_push(pos);
                }
                //sgl_number_set_value(number, myEvent.motion.x);
                break;
            case SDL_MOUSEBUTTONUP:
                if(myEvent.button.button == SDL_BUTTON_LEFT) {
                    sgl_event_pos_t pos;
                    pos.x = myEvent.motion.x; // 获取鼠标X轴位置
                    pos.y = myEvent.motion.y; // 获取鼠标Y轴位置
                    pos.type = SGL_EVENT_RELEASED;
                    sgl_event_queue_push(pos);
                }
                break;
            }
            if(myEvent.type==SDL_KEYDOWN){
                switch(myEvent.key.keysym.sym){
                case SDLK_UP:
                    // sgl_obj_event_click(sw);
                    // sgl_obj_event_press(btn);
                    // bat_qua ++;
                    // sgl_battery_set_quantity(bat, bat_qua);
                    // sgl_obj_add_dirty_list(bat);
                    break;
                case SDLK_DOWN:
                    // sgl_obj_event_release(btn);
                    // bat_qua -- ;
                    // sgl_battery_set_quantity(bat, bat_qua);
                    // sgl_obj_add_dirty_list(bat);
                    break;
                case SDLK_LEFT:
                    // sgl_obj_set_selected(btn, true);
                    // sgl_obj_set_selected(sw, false);
                    // sgl_obj_set_selected(label, false);
                    // sgl_obj_set_selected(chkbox, false);
                    // sgl_obj_set_selected(pgsbar, true);
                    // sgl_obj_set_selected(radio, true);
                    break;
                case SDLK_RIGHT:
                    // sgl_obj_set_selected(radio, false);
                    // sgl_obj_set_selected(pgsbar, false);
                    // sgl_obj_set_selected(label, true);
                    // sgl_obj_set_selected(btn, false);
                    // sgl_obj_set_selected(sw, true);
                    // sgl_obj_set_selected(chkbox, true);
                    break;
                default:
                    break;
                }
            }
            if(myEvent.type == SDL_KEYUP) {
                switch(myEvent.key.keysym.sym){
                case SDLK_UP:
                    //sgl_obj_event_release(btn);
                    break;
                case SDLK_DOWN:
                    
                    break;
                case SDLK_LEFT:
                    break;
                case SDLK_RIGHT:
                    break;
                default:
                    break;
                }

                
            }
        }
    }
	SDL_RemoveTimer(timer);
    SDL_DestroyWindow( m_window );
    SDL_DestroyRenderer( m_renderer );
    SDL_Quit();
    return 1;
}
